package com.ltf.order;

public class ThreadDemo {

    public static void main(String[] args) throws InterruptedException {
        DT dt = new DT();
        Thread t1 = new Thread(dt);
        Thread t2 = new Thread(dt);
        Thread t3 = new Thread(dt);
//        DT t1 = new DT();
//        DT t2 = new DT();
//        DT t3 = new DT();

        t1.start();
        t2.start();
        t3.start();
    }

}


class DT extends Thread {
    private int ticket = 100;

    @Override
    public void run() {
        while (this.ticket > 0) {
            sale();
        }
    }

    public synchronized void sale(){
        if(ticket > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            this.ticket--;
            System.err.println(Thread.currentThread().getName() + " sale one. Now here leaves " + this.ticket);
        }
    }
}

